package com.example.service_aidl_client

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.service_aidl_server.ICarAidlInterface
import com.example.service_aidl_server.ICarCallback

class MainActivity : AppCompatActivity() {
    companion object{
        private const val TAG = "MainActivity"
    }
    private lateinit var mIntent:Intent
    private lateinit var iCarAidlInterface: ICarAidlInterface
    private val connection:ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.d(TAG, "onServiceConnected: ")
            iCarAidlInterface = ICarAidlInterface.Stub.asInterface(service)

        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.d(TAG, "onServiceDisconnected: ")

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        mIntent = Intent()
        mIntent.setAction("com.example.service_aidl_server.ICarAidlInterface")
        mIntent.setPackage("com.example.service_aidl_server")
        val bindService = bindService(mIntent, connection, BIND_AUTO_CREATE)
        Log.d(TAG, "bindService: $bindService ")

    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(connection)
    }

    fun getCurrentSpeed(view: View) {
        val currentSpeed = iCarAidlInterface.currentSpeed
        Log.d(TAG, "getCurrentSpeed: $currentSpeed")
    }

    fun getCurrentSpeed2(view: View) {
        iCarAidlInterface.getCurrentSpeed2(object :ICarCallback.Stub(){
            override fun onCarSpeed(speed: Int) {
                Log.d(TAG, "onCarSpeed: $speed")
            }

        })
    }

    fun number(view: View) {
        var result = arrayOf<String>("","")
        iCarAidlInterface.number("aaa","bbbb",result)
        Log.d(TAG, "number: "+result[0])

    }
}